package com.sk.z.khk.tdd;

import java.util.HashSet;
import java.util.Set;

public class Classifier1 {
	private int number;
	private Set<Integer> factors;

	public Classifier1(int number) throws InvalidNumberException {
		if (number < 0)
			throw new InvalidNumberException(number);
		this.number = number;
		factors = new HashSet<Integer>();
		factors.add(1);
		factors.add(number);

	}

	private boolean isFactor(int factor) {
		return number % factor == 0;
	}

	private Set<Integer> getFactors() {
		return factors;
	}

	private void addFactor(int factor) {
		factors.add(factor);
		factors.add(number / factor);
	}

	private void calculateFactors() {
		for (int i = 2; i <= Math.sqrt(number); i++)
			if (isFactor(i))
				addFactor(i);
	}

	public boolean isPerfect() {
		calculateFactors();
		return sumOfFactors() - number == number;
	}

	private int sumOfFactors() {
		int sum = 0;
		for (int i : factors)
			sum += i;
		return sum;
	}

}
